package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io"
	"os"
)

func main() {
	file, err := os.Create("./src/03_gin/12_log/gin_logger/logger.log")
	if err != nil {
		return
	}
	// os.Stdout代表控制台
	gin.SetMode(gin.ReleaseMode)
	router := gin.New()
	router.Use(gin.Recovery())
	//router.Use(gin.LoggerWithFormatter(logFormatter))
	router.Use(gin.LoggerWithConfig(gin.LoggerConfig{
		Formatter: logFormatter,
		Output:    io.MultiWriter(file, os.Stdout),
		SkipPaths: nil,
	}))
	//router := gin.Default()
	router.GET("/gin-logger", func(c *gin.Context) {
	})
	router.Run()
}

func logFormatter(params gin.LogFormatterParams) string {
	return fmt.Sprintf("[TEST] %s |%s %d %s| %s |%s %s %s| %s \n",
		params.TimeStamp.Format("2006-01-02 15:04:05"),
		params.StatusCodeColor(), params.StatusCode, params.ResetColor(),
		params.ClientIP,
		params.MethodColor(), params.Method, params.ResetColor(),
		params.Path)
}
